#include "viewer_gui.h"


// ----------------------------------------------------------------------------
// Init_File
// ----------------------------------------------------------------------------
void main_GUI::Init_Open_File()
{
	char Name[] = File_Scopy;//"binary4_rectgris2.PNG" CDguide.png
  
	fName = (char *) malloc( strlen(Name) * sizeof(char) );
	strcpy(fName,Name);

	printf("%s\n",fName);
	img_Scopy->SetFileName(fName);

	Scopy_camMapper->SetInput( img_Scopy->GetOutput() );

}

// ----------------------------------------------------------------------------
// Open_File
// ----------------------------------------------------------------------------
void main_GUI::Open_File()
{
	fName = fl_file_chooser("Open File...", "*.png", ".png");
	if(fName == NULL || strlen(fName)<1) 
	{
  		return;
	}
	img_Scopy->SetFileName(fName);

	Scopy_camMapper->SetInput(img_Scopy->GetOutput());
	renderWindow->Render();

}


// ----------------------------------------------------------------------------
// Setting_Img2D
// ----------------------------------------------------------------------------
void main_GUI::Setting_Img2D()
{
	Scopy_camMapper->SetColorWindow(255.0);
	Scopy_camMapper->SetColorLevel(127.5);

	Scopy_camActor->SetWidth(1.0);
	Scopy_camActor->SetHeight(1.0);
	Scopy_camActor->SetPosition(0,0);
	
    Scopy_camActor->SetMapper( Scopy_camMapper );

	Scopy_camActor->GetProperty()->SetDisplayLocationToBackground();
	Scopy_camActor->GetProperty()->SetOpacity(0.5);

	renImage_Scopy->AddActor2D( Scopy_camActor );


}

// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
void main_GUI::leer_centerline2D()
{
	ifstream pFile;

	vtkPoints *Pts = vtkPoints::New();
	vtkCellArray *Lines = vtkCellArray::New();
	Pts->Initialize();
	Lines->Initialize();

/*

	pFile.open("guide_2D_tsi06.txt",ios::in);
	assert (!pFile.fail( )); 

     while (!pFile.eof())      //if not at end of file, continue reading numbers
     {
          cout<<++i<<" "<<u<<" "<<v<<endl;    //print numbers to screen
          pFile >> u;
          pFile >> v;

		  Pts->InsertNextPoint(u-255.0,v-255.0,990.0);
		  Lines->InsertNextCell(2);
		  Lines->InsertCellPoint(i);
		  Lines->InsertCellPoint(++i);
     }
     pFile.close( );       //close file
     assert(!pFile.fail( ));
*/

	//inicialization
	vtkPNGReader * img = vtkPNGReader::New();

	vtkImageData * videoData;
	vtkImageMapper *camMapper = vtkImageMapper::New();
	
	char Name[] = File_Guide2D;//"CDguide.png"
  
	fName = (char *) malloc( strlen(Name) * sizeof(char) );
	strcpy(fName,Name);

	printf("Cd file %s\n",fName);
	img->SetFileName(fName);
	img->Update();
	camMapper->SetInput( img->GetOutput());
	videoData=camMapper->GetInput();

	unsigned char* inPtr, *pp;
	inPtr = (unsigned char*)videoData->GetScalarPointer();
	int dims[3];
	videoData->GetDimensions(dims);
	int w=dims[0];
	int h = dims[1];
	int i,j,k=0;

	for(i=h-9;i>=10;i--)//for(i=10;i<h-10;i++)
		for(j=10;j<w-10;j++)
			if(videoData->GetScalarComponentAsFloat(j,i,0,0)>250.0)//(	inPtr[(j+w*i)] > 250 )
			{
				Pts->InsertNextPoint(-((double)i)*0.6+255.0*0.6,-((double)j)*0.6+255.0*0.6,990.0);
				
				
				if( k>1)
				{
					Lines->InsertNextCell(2);
					Lines->InsertCellPoint(k-1);
					Lines->InsertCellPoint(k);
				}
				k++;
			}

	//extension line
	j = Pts->GetNumberOfPoints();
	double w_p[3], w_m[3], v[3], norm;
	Pts->GetPoint(j-1,w_p);
	Pts->GetPoint(j-10,w_m);

	v[0] = w_p[0] - w_m[0];
	v[1] = w_p[1] - w_m[1];
	v[2] = w_p[2] - w_m[2];

	norm = vtkMath::Norm2D(v);

	for(i=1;i<80;i++)
	{
		w_p[0] = w_p[0] + i*v[0]/norm;
		w_p[1] = w_p[1] + i*v[1]/norm;

		Pts->InsertNextPoint(w_p[0],w_p[1],990.0);
		
		Lines->InsertNextCell(2);
		Lines->InsertCellPoint(k-1);
		Lines->InsertCellPoint(k);
		k++;
	}


	Lunderquist2D->SetPoints(Pts);
	Lunderquist2D->SetLines(Lines);

	Lunderquist2DMapper->SetInput(Lunderquist2D);
	Lunderquist2DActor->SetMapper(Lunderquist2DMapper);
	Lunderquist2DActor->GetProperty()->SetColor(0.50, 0.75 ,0.5);//
	Lunderquist2DActor->GetProperty()->SetOpacity(0.8);
	Lunderquist2DActor->GetProperty()->SetLineWidth(3.0);

	Pts->Delete();
	Lines->Delete();

     return;

  }
// ----------------------------------------------------------------------------
// project
// ----------------------------------------------------------------------------


void  main_GUI::project(vtkPolyData * in_data , vtkPolyData * l_outdata)
{
	l_outdata->DeepCopy(in_data);

		//.MaximumInscribedSphereRadius->Print(std::cout);

	double l_intrinEl[16] = {
		990.0, 0.0, 0.0, 0.0,
		0.0, 990.0, 0.0, 0.0,
		0.0, 0.0, 1.0, 0.0,
		0.0, 0.0, 0.0, 0.0 };

	vtkMatrix4x4 * l_intrin = vtkMatrix4x4::New();
	l_intrin->DeepCopy(l_intrinEl);

	double pt[4];
	double * temp;

	for ( int i = 0 ; i < l_outdata->GetNumberOfPoints() ; i++ )
	{
		l_outdata->GetPoint(i,pt);
		pt[3] = 1;
		temp = l_intrin->MultiplyDoublePoint(pt);
		temp[0] = ( temp[0] / temp[2] );
		temp[1] = ( temp[1] / temp[2] );
		temp[2] = l_intrin->GetElement(0,0);
		l_outdata->GetPoints()->SetPoint(i,temp);
	}
				
	l_intrin->Delete();
	return;

}
